//
//  AddAndEditAdressViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/28.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AddAndEditAdressViewModel.h"
#import "AdressInputTableViewCell.h"
#import "AdressSelectTableViewCell.h"
#import "AdressInforTextViewTableViewCell.h"
#import "AdressFootView.h"
@interface AddAndEditAdressViewModel ()<UITableViewDataSource,UITableViewDelegate>{
    AdressFootView * footView;
    NSArray * lefPlaceholderArr;
    //
}

@property (nonatomic,strong) MyAdressModel_Infos * adress_inforModel;
@end

@implementation AddAndEditAdressViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView theAdresModel:(nonnull MyAdressModel_Infos *)ad_Model{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        if (ad_Model) {
            self.adress_inforModel = ad_Model;
        }else{
            self.adress_inforModel = [MyAdressModel_Infos new];
        }
        lefPlaceholderArr = @[@"请输入收货人姓名",@"请输入收货人手机号码",@"请填写固定电话，选填",@"选择所在地区"];
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [self createHeaderView];
        tableView.tableFooterView = [self createFootView];
        [superView addSubview:tableView];
        [self inputTextFieleChange];
    }
    return self;
}

-(UIView *)createHeaderView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}

-(UIView *)createFootView{
    footView = [[AdressFootView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(110))];
    [[footView.severBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        kKeyboardEndEditing
        DLog(@"c保存");
        [self addAdressRequest];
    }];
    return footView;
}

#pragma mark -- 数据请求
-(void)addAdressRequest{
    NSDictionary * dic_sen = @{@"userId":koString(self.userId),
                           @"warehouseName":koString(self.adress_inforModel.warehouseName),
                           @"consignee":koString(self.adress_inforModel.consignee),
                           @"province":koString(self.adress_inforModel.province),
                           @"city":koString(self.adress_inforModel.city),
                           @"country":koString(self.adress_inforModel.country),
                           @"address":koString(self.adress_inforModel.address),
                           @"phone":koString(self.adress_inforModel.phone),
                           @"tel":koString(self.adress_inforModel.tel),
                           @"sign":@(self.adress_inforModel.sign),
                           @"id":@(self.adress_inforModel.adress_id)};
    NSString * apiStr;
    AddOrEditType * type;
    if (self.adress_inforModel.adress_id>0) {
        apiStr = kEditwarehouseAddress;
        type = AddOrEditType_Edit;
    }else{
        apiStr = kIncreasewarehouseAddress;
        type = AddOrEditType_Add;
    }
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,apiStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic_sen success:^(id  _Nullable response) {
        BaseModel * bModle = [BaseModel yy_modelWithJSON:response];
        if (bModle.code == 0) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(adressEditSuccess:theAddEdit:)]) {
                [self.delegate adressEditSuccess:bModle.data theAddEdit:type];
            }
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}

-(void)deleteAdress{//kDelwarehouseAddressbyid
    NSDictionary *deleDic = @{@"id":@(self.adress_inforModel.adress_id)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kDelwarehouseAddressbyid] lodingView:nil lodingTextString:@"" isLoding:YES parameters:deleDic success:^(id  _Nullable response) {
        BaseModel * b_model = [BaseModel yy_modelWithJSON:response];
        if (b_model.code == 0) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(delegateSuccess:)]) {
                [self.delegate delegateSuccess:b_model.data];
            }
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark -- UITableViewDataSource,UITableViewDelegate>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 4) {
        return kHeightProportion_In_iPhoneTriploid(60);
    }else{
        return kHeightProportion_In_iPhoneTriploid(44);
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell = [BaseTableViewCell cellWithTableView:tableView];
    if (indexPath.row < 4) {
        AdressInputTableViewCell * cell_input = [AdressInputTableViewCell cellWithTableView:tableView];
        cell_input.inputTextField.placeholder = lefPlaceholderArr[indexPath.row];
        switch (indexPath.row) {
            case 3:{
                cell_input.inputTextField.userInteractionEnabled = NO;
                cell_input.showPush = YES;
            }
                break;
            case 1:
            case 2:{
                cell_input.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
                cell_input.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
            }
                break;
            case 0:{
                cell_input.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
            }
                break;
                
            default:
                break;
        }
        cell_input.rowNumPath = indexPath;
        cell_input.in_Model = self.adress_inforModel;
        cell_input.Block_AdressChange = ^(NSString * _Nonnull inputStr) {
            if (indexPath.row == 0) {
                self.adress_inforModel.consignee = inputStr;
            }else if (indexPath.row == 1){
                self.adress_inforModel.phone = inputStr;
            }else{
                self.adress_inforModel.tel = inputStr;
            }
            [self inputTextFieleChange];
        };

        cell = cell_input;
    }else{
        if (indexPath.row == 5) {
            AdressSelectTableViewCell * cell_switch = [AdressSelectTableViewCell cellWithTableView:tableView];
            cell_switch.inputTextField.hidden = YES;
            cell_switch.showSwitch = YES;
            cell_switch.switchSelect = self.adress_inforModel.sign==1?YES:NO;
            cell_switch.leftTitString = @"设为默认地址";
            cell_switch.Block_SwitchACtion = ^(BOOL s_Bool) {
                self.adress_inforModel.sign = s_Bool==YES?1:2;
            };
            cell = cell_switch;
        }else{
            AdressInforTextViewTableViewCell * cell_infor = [AdressInforTextViewTableViewCell cellWithTableView:tableView];
            cell_infor.input_str = self.adress_inforModel.address;
            cell_infor.Block_TextViewChange = ^(NSString * _Nonnull inputText) {
                self.adress_inforModel.address = inputText;
                [self inputTextFieleChange];
            };
            cell = cell_infor;
        }
    }
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(pushAdressSelect)]) {
            [self.delegate pushAdressSelect];
        }
    }
}

-(void)inputTextFieleChange{
    if (self.adress_inforModel.consignee.length>0 && self.adress_inforModel.phone.length>0 && self.adress_inforModel.tel.length>0 && self.adress_inforModel.address.length>0) {
        footView.severBut.backgroundColor = kColor_Rule_Yellow;
        footView.severBut.enabled = YES;
    }else{
        footView.severBut.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        footView.severBut.enabled = NO;
    }
}
@end

